package com.opencloud.tenant.server.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.opencloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.opencloud.common.security.OpenAuthority;
import com.opencloud.common.security.OpenSecurityConstants;
import com.opencloud.common.utils.StringUtils;
import com.opencloud.common.utils.WebUtils;
import com.opencloud.tenant.client.model.SysUserAccount;
import com.opencloud.tenant.client.model.entity.AdminAccount;
import com.opencloud.tenant.client.model.entity.SysRole;
import com.opencloud.tenant.client.model.entity.SysUser;
import com.opencloud.tenant.server.mapper.SysUserMapper;
import com.opencloud.tenant.server.service.AdminAccountService;
import com.opencloud.tenant.server.service.SysRoleService;
import com.opencloud.tenant.server.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 租户用户表 服务实现类
 *
 * @author yhg
 * @date 2021-03-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AdminAccountService adminAccountService;
    @Autowired
    private SysRoleService sysRoleService;


    @Override
    public SysUserAccount login(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        Map<String, String> parameterMap = WebUtils.getParameterMap(WebUtils.getHttpServletRequest());
        // 第三方登录标识
        String loginType = parameterMap.get("login_type");

        AdminAccount adminAccount =null;
        // 邮箱登陆
        if (StringUtils.matchEmail(account)) {
            adminAccount = adminAccountService.getAccount(account, "email");
        }
        if(adminAccount==null){
            return null;
        }
        // 获取用户详细信息
        // 用户权限信息
        SysUserAccount userAccount = getTenantUserAccount(adminAccount.getUserId());
        // 复制账号信息
        BeanUtils.copyProperties(adminAccount, userAccount);
        return userAccount;

    }
    @Override
    public SysUserAccount getTenantUserAccount(Long userId) {

        // 用户权限列表
        List<OpenAuthority> authorities = Lists.newArrayList();

        // 用户角色列表
        List<Map> roles = Lists.newArrayList();
        //查询系统用户资料
        SysUser  sysUser = getSysUserById(userId);

            List<SysRole> rolesList = sysRoleService.getSysUserRoles(userId);
            if (rolesList != null) {
                for (SysRole role : rolesList) {
                    Map roleMap = Maps.newHashMap();
                    roleMap.put("roleId", role.getId());
                    roleMap.put("roleCode", role.getRoleAlias());
                    roleMap.put("roleName", role.getRoleName());
                    // 用户角色详情
                    roles.add(roleMap);
                    // 加入角色标识
                    OpenAuthority authority = new OpenAuthority(role.getId().toString(), OpenSecurityConstants.AUTHORITY_PREFIX_ROLE + role.getRoleAlias(), null, "role");
                    authorities.add(authority);
                }
            }


        // 加入租户 权限/ 从网关查询 用户的租户 权限
        // List<OpenAuthority> userGrantedAuthority = baseAuthorityService.findAuthorityByUser(userId, CommonConstants.ROOT.equals(baseUser.getUserName()));
        SysUserAccount userAccount = new SysUserAccount();

        userAccount.setTenantId(sysUser.getTenantId());
        // 昵称
        userAccount.setNickName(sysUser.getRealName());
        // 头像
        userAccount.setAvatar(sysUser.getAvatar());
        // 权限信息
        userAccount.setAuthorities(authorities);
        userAccount.setRoles(roles);
        //租户用户类型
        userAccount.setUserType(sysUser.getUserType());

        return userAccount;

    }

    @Override
    public SysUser getSysUserById(Long userId) {
        return sysUserMapper.selectById(userId);
    }
}
